<?php
/**
 * 验证码管理
 * @author lzy <1029460965@qq.com>
 */

namespace app\common\model;
use think\Model;
class Verify extends Model
{
    /**
     * 添加验证
     * @param  [string] $val  [邮箱或手机号]
     * @param  [string] $code [验证码]
     * @param  [string] $type [true：手机  false：邮箱]
     * @param  [string] $uid [用户id 一般用户邮箱注册等]
     * @return [array]       [状态码]
     */
    public function checkVer($val,$code){
        $st = time();
        if(checkTel($val)){
            //手机号
            $add["tel"] = $val;
            $where['tel'] = $val;
        }else{

            $data["code"] = 0;
            $data["msg"] = "手机号格式不正确";
            return $data;
        }

        $res = $this->getOne($add);
        $add["code"] = $code;
        $add["st"] = $st;
        if($res){
            //如果先判断次数以及时间,存在则进行修改
            $checktime = config("CHECK_OUT_TIME");
            $num = config("CHECK_NUM");
            if($st-$res["st"]>$checktime*60){
                //大于则更新
                $add["num"] = 1;
                $data["code"] = 1;
                $data["msg"] = "成功";

                self::where($where)->update($add);
            }else{
                if($num>$res["num"]){
                    $add["num"] = $res["num"]+1;
                    $data["code"] = 1;
                    $data["msg"] = "成功";

                    self::where($where)->update($add);
                }else{
                    $data["code"] = 0;
                    $data["msg"] = "超出一定时间所发次数";
                }
            }
        }else{
            //不存在进行添加
            $add["num"] = 1;
            self::insert($add);

            $data["code"] = 1;
            $data["msg"] = "成功";
        }
        return $data;
    }



    /**
     * 验证验证码
     * @param  [string] $val  [邮箱或手机号]
     * @param  [string] $code [验证码]
     * @param  [string] $type [true：手机  false：邮箱]
     * @return [array] [状态码]
     */
    public function Ver($val,$code){
        $st = time();
        if(checkTel($val)){
            //手机号
            $where=[
                'tel'=>$val
            ];
        }else{
            $data["code"] = 0;
            $data["msg"] = "手机号格式不正确";
            return $data;
        }
        $res = $this->getOne($where,'code,st');
        if($code==$res["code"]){
            //验证码是否过期
            $otime = config("CHECK_TIME");
            if($st-$res["st"]<$otime*60){
                $data["code"] = 1;
                $data["msg"] = "验证成功";
            }else{
                $data["code"] = 0;
                $data["msg"] = "验证码过期";
            }
        }else{
            $data["code"] = 0;
            $data["msg"] = "验证码不对";
        }
        return $data;
    }

    /**
     * 删除验证码
     * @param  [array] $val [手机号/邮箱]
     */
    public function delVer($val){
        if(checkTel($val)){
            //手机号
            $where=[
                'tel'=>$val
            ];
        }
        return self::where($where)->delete();
    }

    /**
     * 查询单条信息
     */
    public function getOne($where,$field="*"){
        $result = self::field($field)->where($where)->find();
        if($result){
            $result = $result->getData();
        }
        return $result;
    }

    /**
     * 发送验证码
     * @param  [string] $name [手机号]
     * @param [bool] $type [注册模板 true / 身份验证模板 false]
     * @return [array]           [状态码]
     */
    public function sendTelCode($name, $type=false){
        
        if(!checkTel($name)){
            show(false,'账号格式不正确');
        }

        $code = rand(100000,999999);
        //手机
        $data = $this->checkVer($name,$code);
        if($data["code"]){
            $res = send_sms($name, $code,$type);
            if($res){
                $return["code"] = true;
                $return["msg"] = "发送成功";
            }else{
                $return["code"] = false;
                $return["msg"] = "发送失败";
            }
        }else{
            $return = $data;
        }
        return $return;
    }
}